﻿using UnityEngine;

public enum WaitType
{
    UnWait = 1,
    WaitUI = 2,
    WaitMsg = 4,
    WaitModle = 8,
}

public class LoadingWaitMgr : IZUpdate
{
    private Transform trans;
    WaitType type = WaitType.UnWait;

    private bool lockScreen;                    //是否出现等待加载锁屏
    private float lockScreenTimer;              //当前计时（秒）
    private float lockDelayTime = 5.0f;         //锁屏出现需求等待时间 (秒)        

    public LoadingWaitMgr()
    {
        trans = AllUpdate.GetChildByName("WaitLoad");
        Utility.SetActive(trans, false);
    }

    public void OpenLoadingWait(WaitType _type, string key = "")
    {
        type |= _type;
        if (!lockScreen)
        {
            UIInputMgr.GetSingleton().LockInput(LockType.LoadingWait);
            EDebug.Log("打开：" + LockType.LoadingWait + "  " + _type + "  " + key);
            lockScreen = true; lockScreenTimer = 0;
            UpdateMgr.GetSingleton().AddUpdate(this);
        }
    }

    public void CloseLoadingWait(WaitType _type, string key = "")
    {
        type ^= _type;
        if (type == WaitType.UnWait)
        {
            if (lockScreen)
            {
                EDebug.Log("关闭：" + LockType.LoadingWait + "  " + _type + "  " + key);
                EndWaitLoading();
            }
        }
    }

    private void EndWaitLoading()
    {
        UIInputMgr.GetSingleton().UnLockInput(LockType.LoadingWait);
        lockScreen = false; lockScreenTimer = 0;
        ZMgr.UIMgr.SetCompleteStatus(true, false);
        Utility.SetActive(trans, false);
        UpdateMgr.GetSingleton().RemoveUpdate(this);
    }

    public void CloseWaitLoading()
    {
        EndWaitLoading();
        type = WaitType.UnWait;
    }

    public void ZUpdate()
    {
        if (lockScreen)
        {
            lockScreenTimer += Time.deltaTime;
            if (lockScreenTimer >= 0.4f)
            {
                lockScreenTimer = 0;
                Utility.SetActive(trans, true);
                UpdateMgr.GetSingleton().RemoveUpdate(this);
                ZMgr.TimerMgr.SetTimeOut(HandleClose, lockDelayTime);
            }
        }
    }

    public void HandleClose()
    {
        if (lockScreen)
        {
            CloseWaitLoading();
        }
    }
}